package com.tinem.platform.module.starter.sdk.core;

import com.tinem.platform.module.pojo.co.GatewayHeadName;
import com.tinem.platform.module.starter.sdk.MessageCode;
import com.tinem.platform.module.starter.sdk.config.GatewayConfig;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author fzh
 * @version v1
 * @program: platform
 * @className ApiUtil
 * @description TODO
 * @site 
 * @company 
 * @create 2020-09-04 15:33
 */
@Slf4j
public abstract class BaseApi {
    String device = UUID.randomUUID().toString();
    @Resource
    GatewayConfig gatewayConfig;
    @Resource
    TokenManager tokenManager;

    public JSONObject post(String module, String api, Object body){
        long startTime = System.currentTimeMillis();
        HttpRequest request = HttpUtil.createPost(StrUtil.format(gatewayConfig.getUrl(), startTime));
        request.header(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_SERVICE,module);
        request.header(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_METHOD,api);
        request.header(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_VERSION,"0.0.1-SNAPSHOT");
        request.header(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_API_REQUEST_ID, UUID.randomUUID().toString());
        request.header(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_CHARSET,"UTF-8");
        request.header(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_LANG,"en-us");
        request.header(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_DEVICE,device);
        request.header(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_JWT,tokenManager.getAccessToken());
        request.header("Authorization",StrUtil.format("Bearer {}",tokenManager.getAccessToken()));
        request.body(JSON.toJSONString(body),"application/json");
        log.info("req id:({}); service:({}); method:({});"
                ,request.header(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_API_REQUEST_ID)
                ,request.header(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_SERVICE)
                ,request.header(GatewayHeadName.X_PLATFORM_GATEWAY_REQ_METHOD)
        );
        HttpResponse httpResponse = request.execute();
        long endTime = System.currentTimeMillis();
        log.info("execute time start({}) end({}) continued:({});",startTime,endTime,endTime-startTime);
        log.debug("httpResponse head:{}:{}",GatewayHeadName.X_PLATFORM_GATEWAY_RES_CODE,httpResponse.header(GatewayHeadName.X_PLATFORM_GATEWAY_RES_CODE));
        log.debug("httpResponse head:{}:{}",GatewayHeadName.X_PLATFORM_GATEWAY_RES_MESSAGE,httpResponse.header(GatewayHeadName.X_PLATFORM_GATEWAY_RES_MESSAGE));
        log.debug("httpResponse head:{}:{}",GatewayHeadName.X_PLATFORM_RES_CODE,httpResponse.header(GatewayHeadName.X_PLATFORM_RES_CODE));
        log.debug("httpResponse head:{}:{}",GatewayHeadName.X_PLATFORM_RES_MESSAGE,httpResponse.header(GatewayHeadName.X_PLATFORM_RES_MESSAGE));
        log.debug("httpResponse head:{}:{}",GatewayHeadName.X_PLATFORM_GATEWAY_REQUEST_ID,httpResponse.header(GatewayHeadName.X_PLATFORM_GATEWAY_REQUEST_ID));
        // 请求正常
        if(httpResponse.getStatus() != 200){
            log.error("http status:({});Http body:{}",httpResponse.getStatus(),httpResponse.body());
            throw new HttpException(httpResponse.getStatus()+"");
        }
        // 网关状态码正确
        if(!MessageCode.SUCCESS.name().equalsIgnoreCase(httpResponse.header(GatewayHeadName.X_PLATFORM_GATEWAY_RES_CODE))){
            log.error("gateway code:({}); message:{}"
                    , httpResponse.header(GatewayHeadName.X_PLATFORM_GATEWAY_RES_CODE)
                    , httpResponse.header(GatewayHeadName.X_PLATFORM_GATEWAY_RES_MESSAGE));
            throw new HttpException(httpResponse.getStatus()+"");
        }
        // 业务状态码正常
        if(!MessageCode.SUCCESS.name().equalsIgnoreCase(httpResponse.header(GatewayHeadName.X_PLATFORM_RES_CODE))){
            log.error("gateway code:({}); message:{}"
                    , httpResponse.header(GatewayHeadName.X_PLATFORM_RES_CODE)
                    , Base64.decode(httpResponse.header(GatewayHeadName.X_PLATFORM_RES_MESSAGE)));
            throw new HttpException(httpResponse.getStatus()+"");
        }
        return JSON.parseObject(httpResponse.body());
    }
}
